use thiserror_no_std::Error;

use crate::c::rtx_os::{
    osFlagsErrorISR, osFlagsErrorParameter, osFlagsErrorResource, osFlagsErrorTimeout,
    osFlagsErrorUnknown, osStatus_t_osErrorISR, osStatus_t_osErrorNoMemory,
    osStatus_t_osErrorParameter, osStatus_t_osErrorResource, osStatus_t_osErrorTimeout,
    osStatus_t_osOK,
};

#[derive(Debug, Error)]
pub enum Error {
    Unknown,
    Parameter,
    Resource,
    ISR,
    Timeout,
    NoMemory,
    StatusReserved,
}

pub fn parse_flags_error(ret: u32) -> Result<(), Error> {
    match ret {
        osFlagsErrorUnknown => Err(Error::Unknown),
        osFlagsErrorParameter => Err(Error::Parameter),
        osFlagsErrorResource => Err(Error::Resource),
        osFlagsErrorISR => Err(Error::ISR),
        osFlagsErrorTimeout => Err(Error::Timeout),
        _ => Ok(()),
    }
}

pub fn parse_error<T>(v: T, ret: i32) -> Result<T, Error> {
    match ret {
        osStatus_t_osOK => Ok(v),
        osStatus_t_osErrorParameter => Err(Error::Parameter),
        osStatus_t_osErrorResource => Err(Error::Resource),
        osStatus_t_osErrorISR => Err(Error::ISR),
        osStatus_t_osErrorTimeout => Err(Error::Timeout),
        osStatus_t_osErrorNoMemory => Err(Error::NoMemory),
        _ => Err(Error::StatusReserved),
    }
}
